/**
 * Copyright 2002 Cordys R&D B.V. 
 * 
 * This file is part of the Cordys .NET Connector. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 /***************************************************************************
	
***************************************************************************/

#include "xmlForDotnet.h"
#include "Unmanaged.h"

using namespace System;
using namespace com::eibus::xml::nom;
using namespace System::Runtime::InteropServices;

/*
 * Function converts the Unicode string into UTF-8 byte array.
 * Because as native code char is only a 8 bit byte whereas in .Net it is 16 bit.
 * Releasing of this refernece is not required because the GC will take of 
 * releasing.
 */
#pragma managed

/** Converts the sequence of Characters to the ByteArray and returns the 
 *  no of bytes converted 
 */
Int32 GetByteFromChar(const char* srcChar ,Byte destinationBytes[],int charLength )  
{
	System::Text::UTF8Encoding * ptrCls = new System::Text::UTF8Encoding();		
	return (ptrCls->GetBytes(srcChar,0,charLength,destinationBytes,0));
}

/** Converts the String to the UTF-8 char array and returns the array 
 */ 
unsigned char __gc * GetUTF8CharForStr(String __gc * Mstring)
{	
	    Byte bytes __gc[] = NULL;
		try
		{	
			System::Text::UTF8Encoding * ptrCls = new System::Text::UTF8Encoding();
			bytes = ptrCls->GetBytes(Mstring);			
			return &(bytes)[0];
		}
		catch(Exception* e)
		{
			if(bytes->Count==0)
			{
			   new XMLException(S"Line: 1;Col: 0; unclosed token");
			}
			else
			{
			   new XMLException(e->Message);
			}
		}
}

/** Returns the UTF-8 Converted String
 */
String __gc * GetUTF8String(unsigned char prefix __gc[])
{
	System::Text::UTF8Encoding * ptrCls = new System::Text::UTF8Encoding();
	return (ptrCls->GetString(prefix));
}

/** Returns the Byte array length if the String is converted to the Array
 */
int GetByteLength(String __gc * Mstring)
{
	System::Text::UTF8Encoding * ptrCls = new System::Text::UTF8Encoding();
	return (ptrCls->GetBytes(Mstring))->Count;
}

/** Returns the Byte array from the String
 */
Byte  GetByteFromStr(String __gc * Mstring) __gc[]
{
	System::Text::UTF8Encoding * ptrCls = new System::Text::UTF8Encoding();	
	return (ptrCls->GetBytes(Mstring));
}

/*
 * Converts the Byte array to String and returns
 */
String * GetStrFromByteArray(Byte  dataBytes __gc[])
{
	System::Text::UTF8Encoding * ptrCls = new System::Text::UTF8Encoding();	
	return (ptrCls->GetString(dataBytes));
}

/*
 * Releasing the pinning of pointer , so that garbageCollector can take care of that object.
 */
void ReleasePINPointer(void * pinPointer)
{	
	pinPointer = 0;
}